import graphene
from flask import Flask
from flask_graphql import GraphQLView

users =[]
class Person(graphene.ObjectType):
    name = graphene.String()
    age = graphene.Int()

class CreatePerson(graphene.Mutation):
    person = graphene.Field(Person)
    msg = graphene.String()
    class Arguments:    # 设置可以传进来的参数
        name = graphene.String()
        age = graphene.Int()

    def mutate(self,info,name,age):
        # 存储的逻辑
        p = Person(name =name,age = age)
        users.append(p)
        return  CreatePerson(person = p,msg="success")# 响应请求的结果
# Mutation
class Mutation(graphene.ObjectType):    # 映射对外使用字段
    create_person = CreatePerson.Field()

class Query(graphene.ObjectType):
    persons = graphene.List(Person)

    def resolve_persons(self,info):
        return users
if __name__ =='__main__':
    schema = graphene.Schema(query=Query,mutation=Mutation)
    app = Flask(__name__)
    app.add_url_rule('/graphql',view_func=GraphQLView.as_view('grapql',schema=schema, graphiql=True))
    app.run(debug=True)